/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uit_vendor_management;

import java.awt.Color;
import java.awt.Component;
import java.sql.Date;
import javax.swing.table.*;


/**
 *
 * @author Minh Nguyen
 */
public class LookupForm extends javax.swing.JFrame {
    /**
     * Properties of LookupForm
     */
    String MaDaiLy = UpperFirstChar(Language._("LOOKUP_FORM_MA_DAI_LY_LBL")).toString();
    String Ten = UpperFirstChar(Language._("LOOKUP_FORM_TEN_LBL")).toString();
    String DienThoai = UpperFirstChar(Language._("LOOKUP_FORM_DIEN_THOAI_LBL")).toString();
    String DiaChi = UpperFirstChar(Language._("LOOKUP_FORM_DIA_CHI_LBL")).toString();
    String NgayTiepNhan = UpperFirstChar(Language._("LOOKUP_FORM_NGAY_TIEP_NHAN_COLUMN_TITLE")).toString();
    String Email = "Email";
    String LoaiDaiLy = UpperFirstChar(Language._("LOOKUP_FORM_LOAI_DAI_LY_LBL")).toString();
    String Quan = UpperFirstChar(Language._("LOOKUP_FORM_QUAN_LBL")).toString();
    String BeginQuery = 
            "SELECT DAILY.MaDaiLy as '" + MaDaiLy + "', "
            + "DAILY.TenDaiLy as '" + Ten + "', "
            + "DAILY.DienThoai as '" + DienThoai + "', "
            + "DAILY.DiaChi as '" + DiaChi + "', "
            + "NgayTiepNhan as '" + NgayTiepNhan + "', "
            + "DAILY.Email as '" + Email + "', "
            + "LOAIDAILY.TenLoaiDaiLy as '" + LoaiDaiLy + "', "
            + "QUAN.TenQuan as '" + Quan + "' FROM DAILY " +
            "INNER JOIN LOAIDAILY ON DAILY.MaLoaiDaiLy = LOAIDAILY.MaLoaiDaiLy " +
            "INNER JOIN QUAN ON DAILY.MaQuan = QUAN.MaQuan " +
            "WHERE ";
    String Query = "";
    Boolean HasAnd = false;
    Boolean FailValue = false;
    
    /**
     * Creates new form LookupForm
     */
    public LookupForm() {
        initComponents();
        /**
         * DefaultTable: 
         *  - Create TitleRows for table
         *  - Don't have DataRows (will be created when click btnTraCuu)
         */
        this.InitLanguage();
        String[] TitleRows;
        TitleRows = new String[] { MaDaiLy, Ten, DienThoai, DiaChi, NgayTiepNhan, Email, LoaiDaiLy, Quan };
        TableModel dtm = new DefaultTableModel(new String[][] {}, TitleRows);
        tbeKetQua.setModel(dtm);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlInfo = new javax.swing.JPanel();
        tfdEmail = new javax.swing.JTextField();
        tfdDiaChi = new javax.swing.JTextField();
        lblEmail = new javax.swing.JLabel();
        tfdLoaiDaiLy = new javax.swing.JTextField();
        lblDiaChi = new javax.swing.JLabel();
        tfdQuan = new javax.swing.JTextField();
        lblLoaiDaiLy = new javax.swing.JLabel();
        lblQuan = new javax.swing.JLabel();
        tfdTen = new javax.swing.JTextField();
        lblTen = new javax.swing.JLabel();
        tfdMaDaiLy = new javax.swing.JTextField();
        lblMaDaiLy = new javax.swing.JLabel();
        tfdDienThoai = new javax.swing.JTextField();
        lblDienThoai = new javax.swing.JLabel();
        lblFromDate = new javax.swing.JLabel();
        lblToDate = new javax.swing.JLabel();
        dcrFromDate = new com.toedter.calendar.JDateChooser();
        dcrToDate = new com.toedter.calendar.JDateChooser();
        pnlResult = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbeKetQua = new javax.swing.JTable();
        lblTraCuuDaiLy = new javax.swing.JLabel();
        btnThoat = new javax.swing.JButton();
        btnTraCuu = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(650, 500));
        setPreferredSize(new java.awt.Dimension(650, 490));
        setResizable(false);

        pnlInfo.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        pnlInfo.setName("pnlInfo"); // NOI18N

        tfdEmail.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        tfdEmail.setName("tfdEmail"); // NOI18N
        tfdEmail.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfdEmailFocusLost(evt);
            }
        });

        tfdDiaChi.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        tfdDiaChi.setName("tfdDiaChi"); // NOI18N

        lblEmail.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        lblEmail.setText("Email");
        lblEmail.setName("lblEmail"); // NOI18N

        tfdLoaiDaiLy.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        tfdLoaiDaiLy.setName("tfdLoaiDaiLy"); // NOI18N
        tfdLoaiDaiLy.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfdLoaiDaiLyFocusLost(evt);
            }
        });

        lblDiaChi.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        lblDiaChi.setText("Địa chỉ");
        lblDiaChi.setName("lblDiaChi"); // NOI18N

        tfdQuan.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        tfdQuan.setName("tfdQuan"); // NOI18N

        lblLoaiDaiLy.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        lblLoaiDaiLy.setText("Loại đại lý");
        lblLoaiDaiLy.setName("lblLoaiDaiLy"); // NOI18N

        lblQuan.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        lblQuan.setText("Quận");
        lblQuan.setName("lblQuan"); // NOI18N

        tfdTen.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        tfdTen.setName("tfdTen"); // NOI18N
        tfdTen.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfdTenFocusLost(evt);
            }
        });

        lblTen.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        lblTen.setText("Tên");
        lblTen.setName("lblTen"); // NOI18N

        tfdMaDaiLy.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        tfdMaDaiLy.setName("tfdMaDaiLy"); // NOI18N
        tfdMaDaiLy.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfdMaDaiLyFocusLost(evt);
            }
        });

        lblMaDaiLy.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        lblMaDaiLy.setText("Mã đại lý");
        lblMaDaiLy.setName("lblMaDaiLy"); // NOI18N

        tfdDienThoai.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        tfdDienThoai.setName("tfdDienThoai"); // NOI18N
        tfdDienThoai.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfdDienThoaiFocusLost(evt);
            }
        });

        lblDienThoai.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        lblDienThoai.setText("Điện thoại");
        lblDienThoai.setName("lblDienThoai"); // NOI18N

        lblFromDate.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        lblFromDate.setText("Ngày tiếp nhận từ:");
        lblFromDate.setName("lblFromDate"); // NOI18N

        lblToDate.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        lblToDate.setText("Đến:");
        lblToDate.setName("lblToDate"); // NOI18N

        dcrFromDate.setName("dcrFromDate"); // NOI18N

        dcrToDate.setName("dcrToDate"); // NOI18N

        javax.swing.GroupLayout pnlInfoLayout = new javax.swing.GroupLayout(pnlInfo);
        pnlInfo.setLayout(pnlInfoLayout);
        pnlInfoLayout.setHorizontalGroup(
            pnlInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlInfoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblTen)
                    .addComponent(lblDienThoai)
                    .addComponent(lblQuan)
                    .addComponent(lblMaDaiLy)
                    .addComponent(lblFromDate))
                .addGap(18, 18, 18)
                .addGroup(pnlInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tfdTen, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(tfdDienThoai, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(tfdQuan)
                    .addComponent(tfdMaDaiLy)
                    .addComponent(dcrFromDate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(pnlInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblLoaiDaiLy)
                    .addComponent(lblDiaChi)
                    .addComponent(lblEmail)
                    .addComponent(lblToDate))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tfdLoaiDaiLy)
                    .addComponent(tfdEmail)
                    .addComponent(tfdDiaChi)
                    .addComponent(dcrToDate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        pnlInfoLayout.setVerticalGroup(
            pnlInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlInfoLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(pnlInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblMaDaiLy)
                    .addComponent(tfdMaDaiLy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTen)
                    .addComponent(tfdTen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblLoaiDaiLy)
                    .addComponent(tfdLoaiDaiLy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblDienThoai)
                    .addComponent(tfdDienThoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblDiaChi)
                    .addComponent(tfdDiaChi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblQuan)
                    .addComponent(tfdQuan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblEmail)
                    .addComponent(tfdEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addGroup(pnlInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblFromDate)
                        .addComponent(lblToDate))
                    .addComponent(dcrFromDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dcrToDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pnlResult.setName("pnlResult"); // NOI18N

        tbeKetQua.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        tbeKetQua.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbeKetQua.setName("tbeKetQua"); // NOI18N
        jScrollPane1.setViewportView(tbeKetQua);

        javax.swing.GroupLayout pnlResultLayout = new javax.swing.GroupLayout(pnlResult);
        pnlResult.setLayout(pnlResultLayout);
        pnlResultLayout.setHorizontalGroup(
            pnlResultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 577, Short.MAX_VALUE)
        );
        pnlResultLayout.setVerticalGroup(
            pnlResultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlResultLayout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        lblTraCuuDaiLy.setFont(new java.awt.Font("Calibri", 0, 48)); // NOI18N
        lblTraCuuDaiLy.setText("TRA CỨU ĐẠI LÝ");

        btnThoat.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        btnThoat.setText("Thoát");
        btnThoat.setName("btnThoat"); // NOI18N
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        btnTraCuu.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        btnTraCuu.setText("Tra Cứu");
        btnTraCuu.setName("btnTraCuu"); // NOI18N
        btnTraCuu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTraCuuActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(80, 80, 80)
                .addComponent(btnTraCuu)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnThoat)
                .addGap(89, 89, 89))
            .addGroup(layout.createSequentialGroup()
                .addGap(152, 152, 152)
                .addComponent(lblTraCuuDaiLy)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(30, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(pnlResult, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnlInfo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(28, 28, 28))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblTraCuuDaiLy, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(pnlInfo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(pnlResult, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnThoat)
                    .addComponent(btnTraCuu))
                .addGap(0, 22, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public final void InitLanguage() 
    {
        this.lblTraCuuDaiLy.setText(Language._("LOOKUP_FORM_TITLE_LBL"));
        this.lblMaDaiLy.setText(Language._("LOOKUP_FORM_MA_DAI_LY_LBL"));
        this.lblTen.setText(Language._("LOOKUP_FORM_TEN_LBL"));
        this.lblLoaiDaiLy.setText(Language._("LOOKUP_FORM_LOAI_DAI_LY_LBL"));
        this.lblDienThoai.setText(Language._("LOOKUP_FORM_DIEN_THOAI_LBL"));
        this.lblDiaChi.setText(Language._("LOOKUP_FORM_DIA_CHI_LBL"));
        this.lblQuan.setText(Language._("LOOKUP_FORM_QUAN_LBL"));
        this.lblFromDate.setText(Language._("LOOKUP_FORM_FROMDATE_LBL"));
        this.lblToDate.setText(Language._("LOOKUP_FORM_TODATE_LBL"));
    }
    
    private StringBuilder UpperFirstChar(String text)
    {
        final StringBuilder result = new StringBuilder(text.length());
        if(text.length() > 1)
        {
            String[] words = text.split("\\s");
            for(int i=0,l=words.length;i<l;++i) {
              if(i>0) result.append(" ");      
              result.append(Character.toUpperCase(words[i].charAt(0)))
                    .append(words[i].substring(1));
            }
        }
        else
        {
            result.append(text.toUpperCase());
        }
        return result;
    }
    private void tfdTenFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfdTenFocusLost
        // TODO add your handling code here:
        String regex = "^[a-zA-Z0-9 đôốồộỗổơớờởỡợưứừửữựâấầẩẫậắằẳẵặêếềểễệáàảãạéèẻẽẹúùủũụíìỉĩịóòỏõọýỳỷỹỵ]+$";
        if(!"".equals(tfdTen.getText()))
            if(tfdTen.getText().matches(regex))
            {
                tfdTen.setBackground(Color.white);
            }
            else
            {
                tfdTen.setBackground(Color.red);
            }
        else
        {
            tfdTen.setBackground(Color.white);
        }
    }//GEN-LAST:event_tfdTenFocusLost

    private void tfdLoaiDaiLyFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfdLoaiDaiLyFocusLost
        // TODO add your handling code here:
        String regex = "^[0-9]+$";
        if(!"".equals(tfdLoaiDaiLy.getText()))
             if(tfdLoaiDaiLy.getText().matches(regex))
             {
                 tfdLoaiDaiLy.setBackground(Color.white);
             }
             else
             {
                 tfdLoaiDaiLy.setBackground(Color.red);
             }
        else
        {
            tfdLoaiDaiLy.setBackground(Color.white);
        }
    }//GEN-LAST:event_tfdLoaiDaiLyFocusLost

    private void tfdDienThoaiFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfdDienThoaiFocusLost
        // TODO add your handling code here:
        String regex = "^[0][0-9]+$";
        if(!"".equals(tfdDienThoai.getText()))
             if(tfdDienThoai.getText().matches(regex))
             {
                 tfdDienThoai.setBackground(Color.white);
             }
             else
             {
                 tfdDienThoai.setBackground(Color.red);
             }
        else
        {
            tfdDienThoai.setBackground(Color.white);
        }
    }//GEN-LAST:event_tfdDienThoaiFocusLost

    private void tfdEmailFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfdEmailFocusLost
        // TODO add your handling code here:
        String regex = "([_A-Za-z0-9-]+)(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})";
        if(!"".equals(tfdEmail.getText()))
             if(tfdEmail.getText().matches(regex))
             {
                 tfdEmail.setBackground(Color.white);
             }
             else
             {
                 tfdEmail.setBackground(Color.red);
             }
        else
        {
            tfdEmail.setBackground(Color.white);
        }
    }//GEN-LAST:event_tfdEmailFocusLost

    private void tfdMaDaiLyFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfdMaDaiLyFocusLost
        // TODO add your handling code here:
        String regex = "^[A-Z][0-9]+$";
        if(!"".equals(tfdMaDaiLy.getText()))
             if(tfdMaDaiLy.getText().matches(regex))
             {
                 tfdMaDaiLy.setBackground(Color.white);
             }
             else
             {
                 tfdMaDaiLy.setBackground(Color.red);
             }
        else
        {
            tfdMaDaiLy.setBackground(Color.white);
        }
    }//GEN-LAST:event_tfdMaDaiLyFocusLost

    private void btnTraCuuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTraCuuActionPerformed
        // TODO add your handling code here:
        //Check value of text fields by FailValue
        FailValue = false;
        for(int i = 0; i < pnlInfo.getComponentCount() && !FailValue; i++)
        {
            Component com = pnlInfo.getComponent(i);
            if("tfd".equals(com.getName().substring(0, 3)))
            {
                if(com.getBackground() == Color.red)
                    FailValue = true;
            }
        }
        
        //if FailValue is true, user cannot query sql
        if(!FailValue)
        {
            Query = BeginQuery;
            if (!"".equals(tfdMaDaiLy.getText()))
            {
                if (HasAnd)
                    Query += "AND DAILY.MaDaiLy LIKE '%" + tfdMaDaiLy.getText() + "%'";
                else
                {
                    Query += "DAILY.MaDaiLy LIKE '%" +  tfdMaDaiLy.getText() + "%'";
                    HasAnd = true;
                }
            }
            if (!"".equals(tfdTen.getText()))
            {
                if (HasAnd)
                    Query += "AND DAILY.TenDaiLy LIKE '%" + tfdTen.getText() + "%'";
                else
                {
                    Query += "DAILY.TenDaiLy LIKE '%" + tfdTen.getText() + "%'";
                    HasAnd = true;
                }
            }
            if (!"".equals(tfdLoaiDaiLy.getText()))
            {
                if (HasAnd)
                    Query += "AND LOAIDAILY.TenLoaiDaiLy LIKE '%" + tfdLoaiDaiLy.getText() + "%'";
                else
                {
                    Query += "LOAIDAILY.TenLoaiDaiLy LIKE '%" + tfdLoaiDaiLy.getText() + "%'";
                    HasAnd = true;
                }
            }
            if (!"".equals(tfdDienThoai.getText()))
            {
                if (HasAnd)
                    Query += "AND DAILY.DienThoai LIKE '%" + tfdDienThoai.getText() + "%'";
                else
                {
                    Query += "DAILY.DienThoai LIKE '%" + tfdDienThoai.getText() + "%'";
                    HasAnd = true;
                }
            }
            if (!"".equals(tfdDiaChi.getText()))
            {
                if (HasAnd)
                    Query += "AND DAILY.DiaChi LIKE '%" + tfdDiaChi.getText() + "%'";
                else
                {
                    Query += "DAILY.DiaChi LIKE '%" + tfdDiaChi.getText() + "%'";
                    HasAnd = true;
                }
            }
            if (!"".equals(tfdQuan.getText()))
            {
                if (HasAnd)
                    Query += "AND QUAN.TenQuan LIKE '%" + tfdQuan.getText() + "%'";
                else
                {
                    Query += "QUAN.TenQuan LIKE '%" + tfdQuan.getText() + "%'";
                    HasAnd = true;
                }
            }
            if (!"".equals(tfdEmail.getText()))
            {
                if (HasAnd)
                    Query += "AND DAILY.Email LIKE '%" + tfdEmail.getText() + "%'";
                else
                {
                    Query += "DAILY.Email LIKE '%" + tfdEmail.getText() + "%'";
                    HasAnd = true;
                }
            }

            try {
                Date from =  new java.sql.Date(dcrFromDate.getDate().getTime());
                if (HasAnd)
                    Query += "AND DAILY.NgayTiepNhan >= '" + from + "'";
                else
                {
                    Query += "DAILY.NgayTiepNhan >= '" + from + "'";
                    HasAnd = true;
                }
            }
            catch (Exception err)
            {
                //dcrFromDate cannot getDate() because user don't choose date
            }

            try {
                Date to = new java.sql.Date(dcrToDate.getDate().getTime());
                if (HasAnd)
                    Query += "AND DAILY.NgayTiepNhan <= '" + to + "'";
                else
                {
                    Query += "DAILY.NgayTiepNhan <= '" + to + "'";
                    HasAnd = true;
                }
            }
            catch (Exception err)
            {
                //dcrToDate cannot getDate() because user don't choose date
            }
            if (!Query.equals(BeginQuery))
            {
                QueryResult result = Database.instance.Query(Query);
                if(result.ReturnCode == 200)
                {
                    tbeKetQua.setModel(result.Table);
                }
                HasAnd = false;
            }   
        }
    }//GEN-LAST:event_btnTraCuuActionPerformed

    private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
        // TODO add your handling code here:
        this.hide();
    }//GEN-LAST:event_btnThoatActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(LookupForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(LookupForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(LookupForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LookupForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LookupForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnThoat;
    private javax.swing.JButton btnTraCuu;
    private com.toedter.calendar.JDateChooser dcrFromDate;
    private com.toedter.calendar.JDateChooser dcrToDate;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblDiaChi;
    private javax.swing.JLabel lblDienThoai;
    private javax.swing.JLabel lblEmail;
    private javax.swing.JLabel lblFromDate;
    private javax.swing.JLabel lblLoaiDaiLy;
    private javax.swing.JLabel lblMaDaiLy;
    private javax.swing.JLabel lblQuan;
    private javax.swing.JLabel lblTen;
    private javax.swing.JLabel lblToDate;
    private javax.swing.JLabel lblTraCuuDaiLy;
    private javax.swing.JPanel pnlInfo;
    private javax.swing.JPanel pnlResult;
    private javax.swing.JTable tbeKetQua;
    private javax.swing.JTextField tfdDiaChi;
    private javax.swing.JTextField tfdDienThoai;
    private javax.swing.JTextField tfdEmail;
    private javax.swing.JTextField tfdLoaiDaiLy;
    private javax.swing.JTextField tfdMaDaiLy;
    private javax.swing.JTextField tfdQuan;
    private javax.swing.JTextField tfdTen;
    // End of variables declaration//GEN-END:variables

}
